package com.shz.appletsapi.service;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.shz.appletsapi.common.util.DateUtils;
import com.shz.appletsapi.model.MinerHeartbeat;
import com.shz.appletsapi.model.po.MineHeartbeat;
import com.shz.appletsapi.model.po.MineHeartbeatRealtime;
import com.shz.appletsapi.model.po.MineVersion;
import com.shz.appletsapi.prop.ShzDefaults;
import com.shz.appletsapi.repository.MineHeartbeatRealtimeRepository;
import com.shz.appletsapi.repository.MineHeartbeatRepository;
import com.shz.appletsapi.repository.MineVersionRepository;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MineHeartbeatService {
	
	@Autowired
	private MineHeartbeatRepository mineHeartbeatRepository;
	
	@Autowired
	private MineHeartbeatRealtimeRepository mineHeartbeatRealtimeRepository;
	
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	@Async
	public void saveMineHeartbeat(MinerHeartbeat miner) {
		MineHeartbeat mine =new MineHeartbeat();
		mine.setTsid(miner.getTsid());
		mine.setCurr_mem(miner.getCurr_mem());
		mine.setCurr_time(miner.getCurr_time());
		mine.setIs_working(miner.getIs_working());
		mine.setCurr_cpurate(miner.getCurr_cpurate());
		mine.setCurr_ram_rate(miner.getCurr_ram_rate());
		mine.setCurr_cpufans(miner.getCurr_cpufans());
		mine.setNowcount(miner.getNowcount());
		mine.setCurr_procnum(miner.getCurr_procnum());
		mine.setCurr_cputemp(miner.getCurr_cputemp());
		mine.setCurr_graph_num(miner.getCurr_graph_num());
		mine.setCurr_graph_buswidth(miner.getCurr_graph_buswidth());
		mine.setCurr_graph_memtype(miner.getCurr_graph_memtype());
		mine.setCurr_graph_temp(miner.getCurr_graph_temp());
		mine.setCurr_graph_fans(miner.getCurr_graph_fans());
		mine.setMiner_version(miner.getMiner_version());
		mine.setDiff(miner.getDiff());
		mine.setCreate_time(new Date());
		mineHeartbeatRepository.save(mine);
		log.info("save heartbeat success");
	}
	
	
	
	@Async
	public void saveMineHeartbeatRealtime(MinerHeartbeat miner) {
		MineHeartbeatRealtime mine =mineHeartbeatRealtimeRepository.findByTsid(miner.getTsid());
		if(mine ==null) {
			mine = new MineHeartbeatRealtime();
		}
		mine.setTsid(miner.getTsid());
		mine.setCurr_mem(miner.getCurr_mem());
		mine.setCurr_time(miner.getCurr_time());
		mine.setIs_working(miner.getIs_working());
		mine.setCurr_cpurate(miner.getCurr_cpurate());
		mine.setCurr_ram_rate(miner.getCurr_ram_rate());
		mine.setCurr_cpufans(miner.getCurr_cpufans());
		mine.setNowcount(miner.getNowcount());
		mine.setCurr_procnum(miner.getCurr_procnum());
		mine.setCurr_cputemp(miner.getCurr_cputemp());
		mine.setCurr_graph_num(miner.getCurr_graph_num());
		mine.setCurr_graph_buswidth(miner.getCurr_graph_buswidth());
		mine.setCurr_graph_memtype(miner.getCurr_graph_memtype());
		mine.setCurr_graph_temp(miner.getCurr_graph_temp());
		mine.setCurr_graph_fans(miner.getCurr_graph_fans());
		mine.setMiner_version(miner.getMiner_version());
		mine.setDiff(miner.getDiff());
		mine.setCreate_time(new Date());
		mineHeartbeatRealtimeRepository.save(mine);
		log.info("save heartbeat-Realtime success");
	}
	
	@SuppressWarnings("unchecked")
	@Async
	public void realTimeCache(String date,String tsid,String diff) {
		//获取心跳当前日期
		Double subdiff=0.00;
		String fmDate=DateUtils.format(DateUtils.parse(date,DateUtils.PATTERN_DATE),DateUtils.PATTERN_DATE);
		if(StringUtils.isBlank(diff)) {
			log.info("diff is blank return");
			return;
		}
		subdiff=Double.valueOf(diff.substring(0, diff.indexOf("k")));	
		String cacheKey=tsid+"_"+fmDate;	
		Map<String,String> resultMap =new HashMap<>();
		DecimalFormat df = new DecimalFormat("######0.00"); 
		if(redisTemplate.hasKey(cacheKey)){
			//有这个key的话 就修改key 把key取出来
			 resultMap= redisTemplate.opsForHash().entries(cacheKey);  
			//拿到算力(单位kh/s)与当天心跳次数
			String nowcount=resultMap.get(ShzDefaults.DEFALUTS_NOWCOUNT);
			String diffs=resultMap.get(ShzDefaults.DEFALUTS_DIFF);
			resultMap.put(ShzDefaults.DEFALUTS_NOWCOUNT, String.valueOf(Integer.valueOf(nowcount)+1));
			resultMap.put(ShzDefaults.DEFALUTS_DIFF, df.format(Double.valueOf(diffs)+subdiff));							
		}else {
			resultMap.put(ShzDefaults.DEFALUTS_NOWCOUNT,ShzDefaults.DEFALUTS_NUM);
			resultMap.put(ShzDefaults.DEFALUTS_DIFF, df.format(subdiff));	
		}	
		redisTemplate.opsForHash().putAll(cacheKey,resultMap);  
		redisTemplate.expire(cacheKey,1,TimeUnit.DAYS);
		log.info("set redis success:"+cacheKey);
		
		
	}
	
	

	

}
